<?php
namespace app\app\controller;

use think\Controller;
use alidysms\api_demo\SmsDemo as AliSms;
use customtool\Utils;

class Sms extends Controller {
    public function createSMSCode($length = 4) {
        $min = pow(10 , ($length - 1));
        $max = pow(10, $length) - 1;
        return rand($min, $max);
    }
    
    // 根据app注册页面传的手机号码获取验证码
    public function send() {
        if(request()->isPost()) {
            $randomCode = $this->createSMSCode($length = 4);
            $tel = input('post.tel');
            $sceneType = input('post.sceneType');
            $ret = model('AppUser')->where('username', '=', $tel)->select();
            $code = $this->getCode($randomCode, $tel, $sceneType, $ret);
        } else {
            $code = 4203;   // 非post请求
        }
        return json_encode($code);
    }
    
    
    public function sendTest() {
        $code = $this->createSMSCode($length = 4);
        $tel = input('post.tel');
        if($tel) {
            set_time_limit(0);
            header('Content-Type: text/plain; charset=utf-8');
            $response =  AliSms::sendSms(
                    $tel,   //手机号码
                    $signName = '阿里云短信测试专用',  // 签名名称
                    $templateCode = 'SMS_135305282', // 模板CODE
                    $code, // 短信模板中字段的值
                    $product = 'dsd', // 短信模板中字段的值
                    $outId = '1234' //流水号
                    );
//            echo "发送短信（sendSms）接口返回结果：\n";
            $arr = Utils::objectToArray($response);
            dump($arr['Code']);
        }
        return $this->fetch();
        //return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
    }
    
    public function test() {
        $ret = model('AppUser')->where('username', '=', '13723601711')->select();
        return count($ret);
    }
    
    public function getCode($randomCode, $tel, $sceneType, $ret) {
        if(count($ret) != 0 && $sceneType == 'reg') {
            // 手机号已注册
            return 4204;
        }else if(count($ret) == 0 && $sceneType == 'setPwd') {
            // 手机未注册
            return 4205;
        } else {
            header('Content-Type: text/plain; charset=utf-8');
            $response =  AliSms::sendSms(
                    $tel,   //手机号码
                    $signName = '阿里云短信测试专用',  // 签名名称
                    $templateCode = 'SMS_135305282', // 模板CODE
                    $randomCode, // 短信模板中字段的值
                    $product = 'dsd', // 短信模板中字段的值
                    $outId = '1234' //流水号
                    );

            // 将返回的对象转化为数组
            $arr = Utils::objectToArray($response);
            if($arr['Code'] == 'OK') {
                $appTelData = [
                    'tel' => $tel,
                    'code' => $randomCode,
                ];
                $appTelId = model('AppUserApply')->add($appTelData);    
                return  4200;   // 发送成功               
            } else if($arr['Code'] == 'isv.MOBILE_NUMBER_ILLEGAL') {
                return  4201;   // 非法手机
            } else {
                return  4202;   // 其他错误
            }
        }
    }
}
